home *** CD-ROM | disk | FTP | other *** search
- Path: tbird2.pad.otc.com.au!grahamd
- From: grahamd@nms.otc.com.au (Graham Dumpleton)
- Newsgroups: comp.lang.c++
- Subject: Re: delete[] and template questions
- Date: 5 Feb 1996 22:48:16 GMT
- Organization: Telstra, Network Management Systems, Paddington, Australia
- Message-ID: <4f61fg$73@tbird2.pad.otc.com.au>
- References: <1996Jan30.165654.2033@iiasa.ac.at> <4enump$qlr@fsuj01.rz.uni-jena.de>
- NNTP-Posting-Host: baldric.pad.otc.com.au
- X-Newsreader: NN version 6.5.0 #1 (NOV)
-
- mkt@isun04.inf.uni-jena.de (Tilo Koerbs) writes:
-
- >> template <class I, class T>
- >> void mVect<I,T>::resize(I new_size) {
- >> T *old = v;
- >> v = new T[new_size]; // T *v is a private member of mVect
- >> int size_of_elem = sizeof(T); // <--- question 3
- >> delete[] old; // <--- questions 1 & 2
- >> }
- >>
- >> I have the following questions:
- >> 1. Is it absolutely robust and portable to delete[] old, even
- >> if a previous call was for new_size == 0 (or if v was allocated
- >> by the ctor for the size == 0) ?
- >> 2. Is it correct to assume that no destructor is called by this statement ?
- >> 3. Is there any risk involved in using sizeof(T) in this statement ?
-
- >1. If you really called new T[x] regardless if x == 0,
- > or if old is a NULL pointer
- > than the answer is YES!
-
- Technically "YES", in reality, not always.
-
- The ATT/USL 3.0.1 compiler, if the class T has a virtual destructor, has
- been seen to core dump when deleting a vector when the size of the vector
- was originally zero. Purify picked up that the compiler was accessing past
- the end of the single byte array which had been allocated to satisfy the
- requirement that allocating an array of size zero still return a valid
- pointer.
-
- Also, some versions of the ATT/USL compiler have been seen to core dump
- when you use delete[] on a null pointer.
-
- Thus, the answer depends on how robust and portable you want to make your
- code.
-
-
- --
- Graham Dumpleton (grahamd@nms.otc.com.au)
-